# -*- coding: utf-8 -*-
"""
Created on Mon Nov 11 11:27:03 2024

@author: zrk
"""

import pandas as pd
import numpy as np

# 设置一个随机数生成种子，保证实验可复现
np.random.seed(42)

arr = np.random.normal(50, 10, 100)
outlier = [100, 150, -10]

# 将正常值和异常值连接起来
all_num = np.concatenate([arr, outlier])

data = {
    'value': all_num
}
df = pd.DataFrame(data)


# 通过百分比，指定数据中较小的5%和较大的5%为异常值
lower = df['value'].quantile(0.05)
upper = df['value'].quantile(0.95)

# 切片筛选出异常值
df_outlier = df[(df['value'] > upper) | (df['value'] < lower)]

# 通过异常值的index，可以删除
df_clear = df.drop(df_outlier.index)

